package zhcms.tool;

import java.util.Hashtable;

public class GyHTMLEncode {
	private GyHTMLEncode() {
		 
    }
 
	protected static synchronized void buildEntityTables() {
        entityTableEncode = new Hashtable<String, String>(ENTITIES.length);
 
        for (int i = 0; i < ENTITIES.length; i += 2) {
            if (!entityTableEncode.containsKey(ENTITIES[i])) {
                entityTableEncode.put(ENTITIES[i], ENTITIES[i + 1]);
            }
        }
    }
 
    public static final String encode(String s) {
        return encode(s, "\n<br>");
    }
 
    public static final String encode(String s, String cr) {
        if (entityTableEncode == null) {
            buildEntityTables();
        }

        if (s == null) {
            return "";
        }
 
        StringBuffer sb = new StringBuffer(s.length() * 2);
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch >= '?' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch == ' ') {
                sb.append(ch);
                continue;
            }
 
            if (ch == '\n') {
                sb.append(cr);
                continue;
            }
 
            String chEnc = encodeSingleChar(String.valueOf(ch));
            if (chEnc != null) {
                sb.append(chEnc);
            } else {
                sb.append("&#");
                sb.append((new Integer(ch)).toString());
                sb.append(';');
            }
        }
        return sb.toString();
    }
 
    public static final String escape(String s) {
        if (entityTableEncode == null) {
            buildEntityTables();
        }
 
        if (s == null) {
            return "";
        }
 
        StringBuffer sb = new StringBuffer(s.length() * 2);
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch >= '?' && ch <= 'Z' || ch >= 'a' && ch <= 'z' || ch == ' ') {
                sb.append(ch);
                continue;
            }
            String chEnc = encodeSingleChar(String.valueOf(ch));
            if (chEnc != null) {
                sb.append(chEnc);
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

 
    private static String encodeSingleChar(String ch) {
        return (String) entityTableEncode.get(ch);
    }

    private static final String[] ENTITIES = {">", "&gt;", "<", "&lt;", 
 
    "[", "&#91;<b>", "]", "</b>&#93;",
 
    "&", "&amp;", "\"", "&quot;", "'", "&#039;", "\\", "&#092;", "\251", "&copy;", "\256", "&reg;"};
 
	private static Hashtable<String, String> entityTableEncode = null;
	
	
	public static final String htmlEncode(int i){
	     if (i=='&') return "&amp;";
	     else if (i=='<') return "&lt;";
	     else if (i=='>') return "&gt;";
	     else if (i=='"') return "&quot;";
	     else return ""+(char)i;
	}

	    

	public static final String htmlEncode(String st){
	     StringBuffer buf = new StringBuffer();
	     for (int i = 0;i<st.length();i++){
	         buf.append(htmlEncode(st.charAt(i)));
	     }
	  return buf.toString();
	}
}
